package com.qf.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsBrandMapper;
import com.qf.pojo.DtsBrand;
import com.qf.service.DtsBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author bss
 * @version V1.0
 * @Project maven
 * @Package com.qf.service.impl
 * @Description:
 * @Date 2022/10/17 19:52
 */
@Service
public class DtsBrandServiceImpl implements DtsBrandService {

    @Autowired
    DtsBrandMapper brandMapper;
    @Override
    public List<Integer> findBrandByAdminId(Integer adminId) {
        QueryWrapper<DtsBrand> wrapper = new QueryWrapper<>();
        wrapper.eq("`admin_id`",adminId);
        wrapper.eq("`deleted`",false);
        List<DtsBrand> brandList = brandMapper.selectList(wrapper);
        List<Integer> list = brandList.stream().map(dtsBrand -> {
            return dtsBrand.getId();
        }).collect(Collectors.toList());

        return list;
    }

    @Override
    public List<Map<String, Object>> findBrandList() {
        LambdaQueryWrapper<DtsBrand> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsBrand::getDeleted,false);
        List<DtsBrand> brandList = brandMapper.selectList(wrapper);

        List<Map<String, Object>> list = brandList.stream().map(dtsBrand -> {
            Map<String, Object> map = new HashMap<>();
            map.put("value", dtsBrand.getId());
            map.put("label", dtsBrand.getName());
            return map;
        }).collect(Collectors.toList());
        return list;
    }

    @Override
    public DtsBrand findById(Integer brandId) {
        LambdaQueryWrapper<DtsBrand> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsBrand::getDeleted,false);
        wrapper.eq(DtsBrand::getId,brandId);
        DtsBrand brand = brandMapper.selectOne(wrapper);
        return brand;
    }

    @Override
    public Map<String, Object> findBrandByPage(Integer page, Integer limit, String sort, String order, String name, Integer id) {
        IPage<DtsBrand> iPage = new Page<>(page,limit);
        QueryWrapper<DtsBrand> wrapper = new QueryWrapper<>();
        if ("desc".equalsIgnoreCase(order)){
            wrapper.orderByDesc(sort);
        }else {
            wrapper.orderByAsc(sort);
        }
        wrapper.eq("deleted",false);
        if (!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if (id!=null){
            wrapper.like("id",id);
        }

        brandMapper.selectPage(iPage,wrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("total",iPage.getTotal());
        map.put("items",iPage.getRecords());

        return map;
    }

    @Override
    public void insert(DtsBrand brand) {
        LambdaQueryWrapper<DtsBrand> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsBrand::getName,brand.getName());
        Integer count = brandMapper.selectCount(wrapper);

        if (count>0){
            throw new RuntimeException("品牌商名称已存在");
        }
        brandMapper.insert(brand);
    }

    @Override
    public void update(DtsBrand brand) {
        LambdaQueryWrapper<DtsBrand> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsBrand::getDeleted,false);
        wrapper.eq(DtsBrand::getId,brand.getId());
        brandMapper.update(brand, wrapper);
    }

    @Override
    public void delete(DtsBrand brand) {
        brand.setDeleted(true);
        brand.setUpdateTime(new Date());
        brandMapper.updateById(brand);
    }
}
